<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // var str = "asafafaea";
        // var index = str.indexOf('a');
        // var num = 0;
        // while (index != -1) {
        //     console.log(index);
        //     index = str.indexOf('a', index + 1);
        //     num++;
        // }
        // console.log("a" + "出现的次数是" + num);
        function Str(arr) {
            var str = prompt("请输入字母");
            var index = arr.indexOf(str);
            var num = 0;
            while (index != -1) {
                console.log(index);
                index = arr.indexOf('red', index + 1);
                num++;
            }
            return "出现了" + num;
        }
        var arr = Str(["red", "blue", "red", "blue"]);
        console.log(arr);
        // var arr = ["red", "blue", "red", "blue"];
        // var index = arr.indexOf('red');
        // var num = 0;
        // while (index != -1) {
        //     console.log(index);
        //     index = arr.indexOf('red', index + 1);
        //     num++;
        // }
        // console.log(num);
    </script>
</head>

<body>

</body>

</html>